package com.cws4cloud.cws.mall.file.biz.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cws4cloud.cws.common.core.constant.ComponentConstant;
import com.cws4cloud.cws.common.oss.core.OssTemplate;
import com.cws4cloud.cws.common.oss.props.OssProperties;
import com.cws4cloud.cws.mall.file.api.entity.Config;
import com.cws4cloud.cws.mall.file.biz.mapper.ConfigMapper;
import com.cws4cloud.cws.mall.file.biz.service.ConfigService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 配置表
 *
 * @author zhulj
 * @date 2021-01-28 11:29:25
 */
@Service
@RequiredArgsConstructor
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService {

	private final RedisTemplate redisTemplate;

	private final OssTemplate ossTemplate;


	@Override
	public OssProperties getOssProperties() {
		redisTemplate.delete(ComponentConstant.OSS_DEFAULT);
		OssProperties oss = new OssProperties();
		//获取默认的code值
		String code = getDefaultConfig().getValue();
		//读取默认code的配置参数
		LambdaQueryWrapper<Config> sysConfigLambdaQueryWrapper = Wrappers.<Config>query().lambda().eq(Config::getCode, code);
		List<Config> sysConfigList = this.baseMapper.selectList(sysConfigLambdaQueryWrapper);
		oss = listToProps(sysConfigList, oss);
		redisTemplate.opsForValue().set(ComponentConstant.OSS_DEFAULT, oss);
		return oss;
	}


	@Override
	public OssProperties getConfigByCode(String code) {
		OssProperties oss = new OssProperties();
		LambdaQueryWrapper<Config> sysConfigLambdaQueryWrapper = Wrappers.<Config>query().lambda().eq(Config::getCode, code);
		List<Config> sysConfigList = this.baseMapper.selectList(sysConfigLambdaQueryWrapper);
		oss = listToProps(sysConfigList, oss);
		//对oss部分字段进行隐藏显示，保护隐私
		oss.setSecretKey(StrUtil.hide(oss.getSecretKey(), 3, 23));
		return oss;
	}


	@SneakyThrows
	@Override
	public boolean saveConfigOss(OssProperties ossProperties, String code) {

		LambdaUpdateWrapper<Config> lsc = Wrappers.<Config>update().lambda()
				.set(Config::getValue, ossProperties.getEndpoint())
				.eq(Config::getCode, code)
				.eq(Config::getCKey, ComponentConstant.OSS_ENDPOINT);
		this.update(lsc);

		lsc = Wrappers.<Config>update().lambda()
				.set(Config::getValue, ossProperties.getCustomDomain())
				.eq(Config::getCode, code)
				.eq(Config::getCKey, ComponentConstant.OSS_CUSTOM_DOMAIN);
		this.update(lsc);

		lsc = Wrappers.<Config>update().lambda()
				.set(Config::getValue, ossProperties.getAccessKey())
				.eq(Config::getCode, code)
				.eq(Config::getCKey, ComponentConstant.OSS_ACCESS_KEY);
		this.update(lsc);

		//如果key包括*号，则表示未有更新，则忽略更新该数据
		if (!ossProperties.getSecretKey().contains(StringPool.ASTERISK)) {
			lsc = Wrappers.<Config>update().lambda()
					.set(Config::getValue, ossProperties.getSecretKey())
					.eq(Config::getCode, code)
					.eq(Config::getCKey, ComponentConstant.OSS_SECRET_KEY);
			this.update(lsc);
		}

		lsc = Wrappers.<Config>update().lambda()
				.set(Config::getValue, ossProperties.getBucketName())
				.eq(Config::getCode, code)
				.eq(Config::getCKey, ComponentConstant.OSS_BUCKET_NAME);
		//更新配置文件至redis
		this.getOssProperties();
		ossTemplate.afterPropertiesSet();
		return this.update(lsc);
	}


	@Override
	public boolean saveDefaultOss(String code) {
		LambdaUpdateWrapper<Config> lsc = Wrappers.<Config>update().lambda()
				.set(Config::getValue, code)
				.eq(Config::getCKey, ComponentConstant.CODE_DEFAULT)
				.eq(Config::getCode, ComponentConstant.OSS_DEFAULT);
		boolean flag = this.update(lsc);
		if (flag) {
			//更新配置文件至redis
			this.getOssProperties();
		}
		return flag;
	}


	@Override
	public String defaultOss() {
		return getDefaultConfig().getValue();
	}

	@Override
	public void clearOss() {
		redisTemplate.delete(ComponentConstant.OSS_DEFAULT);
	}

	/**
	 * 获取默认OSS配置信息
	 *
	 * @return Config对象
	 */
	public Config getDefaultConfig() {
		//获取默认的oss配置
		LambdaQueryWrapper<Config> lsc = Wrappers.<Config>query().lambda().eq(Config::getCode, ComponentConstant.OSS_DEFAULT);
		return this.baseMapper.selectOne(lsc);
	}

	/**
	 * 将list转换为OssProperties
	 *
	 * @param sysConfigList List列表
	 * @param oss           　oss属性
	 * @return OssProperties
	 */
	public OssProperties listToProps(List<Config> sysConfigList, OssProperties oss) {
		//给OssProperties赋值
		for (Config s : sysConfigList) {
			if (s.getCKey().equals(ComponentConstant.OSS_ENDPOINT)) {
				oss.setEndpoint(s.getValue());
			} else if (s.getCKey().equals(ComponentConstant.OSS_CUSTOM_DOMAIN)) {
				oss.setCustomDomain(s.getValue());
			} else if (s.getCKey().equals(ComponentConstant.OSS_ACCESS_KEY)) {
				oss.setAccessKey(s.getValue());
			} else if (s.getCKey().equals(ComponentConstant.OSS_SECRET_KEY)) {
				oss.setSecretKey(s.getValue());
			} else if (s.getCKey().equals(ComponentConstant.OSS_PATH_STYLE_ACCESS)) {
				oss.setPathStyleAccess(Boolean.valueOf(s.getValue()));
			} else if (s.getCKey().equals(ComponentConstant.OSS_BUCKET_NAME)) {
				oss.setBucketName(s.getValue());
			}

		}
		return oss;
	}
}
